<template>
  <div class="image-wrapper">
    <img :src="src" alt="描述图片" @error="handleImageError" v-if="!isError" />
    <span v-else class="icon-placeholder">
      <Icon name="tupianjiazaishibai" color="#ccc" size="35px" />
    </span>
  </div>
</template>

<script>
import Icon from '@/components/common/Icon/index.vue';

export default {
  name: 'CustomImage',
  components: { Icon },
  props: {
    // 图片源地址
    src: {
      type: String,
      required: false,
      default: ''
    }
  },
  data() {
    return {
      isError: false, // 标记是否发生错误
    };
  },
  methods: {
    // 当图片加载失败时调用此方法
    handleImageError() {
      this.isError = true;
    },
  },
  watch: {
  src(newSrc) {
    if (newSrc && newSrc.trim()) {
      this.isError = false;
    }
  }
}
};
</script>

<style scoped>
/* 根据需要自定义样式 */
.image-wrapper {
  display: inline-block;
  width: 100%;
  height: 100%;
  border-radius: 5px;
  overflow: hidden;
}
.image-wrapper img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.icon-placeholder {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
}
.default-icon {
  /* 移除了不存在的SVG文件引用 */
  width: 50px;
  height: 50px;
  background-size: contain;
}
</style>
